package praca.framework;

import java.util.ArrayList;
import java.util.List;

import praca.interfejsy.Input;
import praca.interfejsy.TouchEvent;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;

public class InputGL implements Input{

	ObjectPool<TouchEvent> pool;
	List<TouchEvent> lista = new ArrayList<TouchEvent>(10);
	public ListenerGL listener;
	

	float height;
	float width;
	
	public InputGL(){

		pool = new ObjectPool<TouchEvent>(new ObjectPool.ObjectFactory<TouchEvent>() {

			@Override
			public TouchEvent createObject() {
				return new TouchEventGL();
			}
		},10);
		
		
		
		listener = new ListenerGL();
	}
	

	
	@Override
	public List<TouchEvent> getTouchEvents() {
		return lista;
		
	}
	
	public void free(TouchEvent e){
		pool.free(e);
	}
	
	public class KeyListener implements OnKeyListener{

		
		
		
		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			return event.getKeyCode() != KeyEvent.KEYCODE_BACK;
		}
		
	}
	
	
	public class ListenerGL implements OnTouchListener{

		@Override
		public boolean onTouch(View arg0, MotionEvent event) {
			TouchEventGL touchEvent =(TouchEventGL) pool.getObject();
			
			switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touchEvent.type = TouchEventGL.TOUCH_DOWN;
                break;
            case MotionEvent.ACTION_MOVE:
                touchEvent.type = TouchEventGL.TOUCH_MOVE;
                break;
            case MotionEvent.ACTION_CANCEL:                
            case MotionEvent.ACTION_UP:
                touchEvent.type = TouchEventGL.TOUCH_UP;
                break;
            }
			//Log.d("touch", "Screen height: "+height);
			touchEvent.x = event.getX() * (800 / width);
			touchEvent.y = -(event.getY()-height) * (480 / height);
			
			//touchEvent.x = event.getX();
			//touchEvent.y = -(event.getY()-height);
			lista.add(touchEvent);
			return true;
		}
		
	}//koniec listenera

	
	public void addBackEvent(){
		TouchEventGL touchEvent =(TouchEventGL) pool.getObject();
		
		touchEvent.type = TouchEventGL.TOUCH_UP;
		touchEvent.x = -100;
		touchEvent.y = -100;
		lista.add(touchEvent);
	}
	
	@Override
	public TouchEvent getLastTouchEvent() {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public void setScreenSize(float width, float height) {
		this.width = width;
		this.height = height;
		
	}

}
